package com.aweme.harmony.mindspore.llm.jni

import kotlinx.cinterop.*
import MindSporeLiteLLMInferNative.LLMInfer_Native_Create as LLMInfer_Native_Create
import MindSporeLiteLLMInferNative.LLMInfer_Native_Destroy as LLMInfer_Native_Destroy
import MindSporeLiteLLMInferNative.LLMInfer_Native_Build as LLMInfer_Native_Build

import MindSporeLiteLLMInferNative.LLMInfer_Native_Stop_Generate as LLMInfer_Native_Stop_Generate
import MindSporeLiteLLMInferNative.LLMInfer_Native_Generate as LLMInfer_Native_Generate
import MindSporeLiteLLMInferNative.LLMInfer_Native_Generate_With_Perf_CallBack as LLMInfer_Native_Generate_With_Perf_CallBack

import MindSporeLiteLLMInferNative.LLMInfer_Native_Generate_Async as LLMInfer_Native_Generate_Async
import MindSporeLiteLLMInferNative.LLMInfer_Native_Generate_Is_End as LLMInfer_Native_Generate_Is_End
import MindSporeLiteLLMInferNative.LLMInfer_Native_Get_Generate_Async_Result as LLMInfer_Native_Get_Generate_Async_Result

/**
 * MindSpore Lite LLM推理本地封装
 * 负责Kotlin与C库之间的适配和数据转换，隐藏底层实现细节
 * 使用句柄(Handle)模式管理C++对象生命周期
 */
class MSLiteLLMInferNativeKMP() {
    companion object {
        /**
         * 创建LLM推理器句柄
         * @return 新创建的句柄指针
         */
        fun LLMInfer_Native_Create_Kmp(): CPointer<*>? {
            return LLMInfer_Native_Create();
        }

        /**
         * 销毁LLM推理器句柄及其所有资源
         * @param handle 要销毁的句柄
         */
        fun LLMInfer_Native_Destroy_Kmp(handle: CPointer<*>?): Unit {
            LLMInfer_Native_Destroy(handle)
        }

        /**
         * 构建模型
         * @param handle LLM推理器句柄
         * @param generate_params 生成参数字符串
         * @param model_file_path 模型文件路径
         * @return 错误码（0表示成功）
         */
        fun LLMInfer_Native_Build_Kmp(
            handle: CPointer<*>?,
            generate_params: String?,
            model_file_path: String?,
            is_record_perf_data: Boolean,
        ): Int {
            return LLMInfer_Native_Build(
                handle,
                generate_params,
                model_file_path,
                is_record_perf_data
            )
        }

        /**
         * 停止生成
         * @param handle LLM推理器句柄
         * @return 错误码（0表示成功）
         */
        fun LLMInfer_Native_Stop_Generate_Kmp(handle: CPointer<*>?): Int {
            return LLMInfer_Native_Stop_Generate(handle);
        }

        /**
         * 生成文本
         * @param handle LLM推理器句柄
         * @param input_text 输入文本
         * @param output_text 输出文本缓冲区（需要预先分配）
         * @return 错误码（0表示成功）
         */
        fun LLMInfer_Native_Generate_Kmp(
            handle: CPointer<*>?,
            input_text: String?,
            output_text: CValuesRef<CPointerVar<ByteVar>>?,
        ): Int {
            return LLMInfer_Native_Generate(handle, input_text, output_text);
        }


        /**
         * 生成文本
         * @param handle LLM推理器句柄
         * @param input_text 输入文本
         * @param output_text 输出文本缓冲区（需要预先分配）
         * @param callback 回调函数指针
         * @param callbackKotlinPtr callback pointer
         * @return 错误码（0表示成功）
         */
        fun LLMInfer_Native_Generate_With_CallBack_Kmp(
            handle: CPointer<*>?,
            input_text: String?,
            output_text: CValuesRef<CPointerVar<ByteVar>>?,
            cCallback: CPointer<CFunction<(CPointer<ByteVar>?, data: COpaquePointer?) -> Unit>>?,
            callbackKotlinPtr: COpaquePointer?
        ): Int {
            return LLMInfer_Native_Generate_With_Perf_CallBack(
                handle,
                input_text,
                output_text,
                cCallback,
                callbackKotlinPtr
            );
        }

        fun LLMInfer_Native_Generate_Async_Kmp(handle: CPointer<*>?, input_text: String?): Int {
            return LLMInfer_Native_Generate_Async(handle, input_text);
        }

        fun LLMInfer_Native_Generate_Is_End_Kmp(handle: CPointer<*>?): Boolean {
            return LLMInfer_Native_Generate_Is_End(handle);
        }

        fun LLMInfer_Native_Get_Generate_Async_Result_Kmp(
            handle: CPointer<*>?,
            output_text: CValuesRef<CPointerVar<ByteVar>>?
        ): Int {
            return LLMInfer_Native_Get_Generate_Async_Result(handle, output_text);
        }
    }
}